package com.cly.house.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cly.house.dao.DeptDao;
import com.cly.house.dao.impl.DeptDaoImpl;
import com.cly.house.entity.Dept;
import com.cly.house.uitl.PageInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class DeptServlet
 */
@WebServlet("/DeptController")
public class DeptController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       DeptDao deptDao = new DeptDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeptController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
    
   
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
    	String type = request.getParameter("type");
		if(type!=null) {
			switch (type) {
			case "list":
				list(request,response);
				break;
			case "toAdd":
				toAdd(request,response);
				break;
			case "add":
				add(request,response);
				break;
			case "toUpdate":
				toUpdate(request,response);
				break;
			case "update":
				update(request,response);
				break;
			case "delete":
				delete(request,response);
				break;
			case "listAll":
				listAll(request,response);
				break;
			default:
				break;
			}
		}
	}
	
	private void listAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		List<Dept> list = deptDao.listAll();
		String jsonStr = new ObjectMapper().writeValueAsString(list);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsonStr);
	}

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String current  = request.getParameter("current");
		int pageNum = 1;
		if(current!=null)
			pageNum = Integer.parseInt(current);
		PageInfo<Dept> pageInfo = deptDao.findByPage(pageNum, 3);
		String jsonStr = new ObjectMapper().writeValueAsString(pageInfo);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsonStr);
	}
	protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
			request.getRequestDispatcher("WEB-INF/jsp/dept2/dept_add.jsp").forward(request, response);
		
		
		
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String pname = request.getParameter("dept.pname");
		String premark = request.getParameter("dept.premark");
		Dept dept = new Dept();
		dept.setPname(pname);
		dept.setPremark(premark);
		dept.setPflag(0);
		int count = deptDao.save(dept);
		String jsonStr = new ObjectMapper().writeValueAsString(count);
		response.getWriter().write(jsonStr);
	}
	protected void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pid = Integer.parseInt(request.getParameter("pid"));
		Dept dept = deptDao.updateId(pid);
		response.setCharacterEncoding("utf-8");
		String jsonStr = new ObjectMapper().writeValueAsString(dept);
		response.getWriter().write(jsonStr);
	
	
	}
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pid = Integer.parseInt(request.getParameter("dept.pid"));
		String pname = request.getParameter("dept.pname");
		String premark = request.getParameter("dept.premark");
		Integer pflag = Integer.parseInt(request.getParameter("dept.pflag"));
		Dept dept = new Dept();
		dept.setPid(pid);
		dept.setPflag(pflag);
		dept.setPname(pname);
		dept.setPremark(premark);
		int count = deptDao.update(dept);
		String jsonStr = new ObjectMapper().writeValueAsString(count);
		response.getWriter().write(jsonStr);
	
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pid = Integer.parseInt(request.getParameter("pid"));
		boolean delete = deptDao.delete(pid);
		String jsonStr = new ObjectMapper().writeValueAsString(delete);
		response.getWriter().write(jsonStr);
		
		
	
	
	}
}
